package com.killer.lunbotu;

import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Vector;

public class MainActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private TextView tv_intro;
    private LinearLayout dot_layout;
    private ArrayList<Ad> list = new ArrayList<Ad>();

    //处理自动切换
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            mViewPager.setCurrentItem(mViewPager.getCurrentItem()+1);
            handler.sendEmptyMessageDelayed(0, 2000);
        };
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
        initData();
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.view_Pager);
        tv_intro = (TextView) findViewById(R.id.tv_intro);
        dot_layout = (LinearLayout) findViewById(R.id.dot_layout);
    }

    //处理文字和点的变化
    private void initListener() {
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.i("killer","---position"+position);
                updateIntroAndDot();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {
        list.add(new Ad(R.drawable.a, "巩俐不低俗，我就不能低俗"));
        list.add(new Ad(R.drawable.b, "朴树又回来了，再唱经典老歌引百万人同唱啊"));
        list.add(new Ad(R.drawable.c, "揭秘北京电影如何升级"));
        list.add(new Ad(R.drawable.d, "乐视网TV版大放送"));
        list.add(new Ad(R.drawable.e, "热血屌丝的反杀"));

        initDots();

        mViewPager.setAdapter(new MyPagerAdapter());

        //处理初始位置可以向左滑
        int centerValue = Integer.MAX_VALUE/2;
        int value = centerValue%list.size();
        mViewPager.setCurrentItem(centerValue-value);

        updateIntroAndDot();//此处保证第一页文字显示正常

        handler.sendEmptyMessageDelayed(0, 2000);//自动轮播
    }

    /**
     * 初始化dot点
     */
    private void initDots() {
        for(int i = 0;i<list.size();i++){
            View view = new View(this) ;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(8,8);//两个参数代表宽高
            if (i!=0){
                params.leftMargin = 5;//除了第一个点，左边距
            }
            view.setLayoutParams(params);
            view.setBackgroundResource(R.drawable.selector_dot);
            dot_layout.addView(view);
        }
    }

    //更新文本
    private void updateIntroAndDot() {
        int currentPage = mViewPager.getCurrentItem()%list.size();
        tv_intro.setText(list.get(currentPage).getIntro());

        for (int i = 0;i<dot_layout.getChildCount();i++){
            dot_layout.getChildAt(i).setEnabled(i==currentPage);
        }
    }

    class MyPagerAdapter extends PagerAdapter{

        //返回多少page
        @Override
        public int getCount() {
            return Integer.MAX_VALUE;//设置成整数最大值，方便轮播
        }

        /**
         * @param view 当前活动的view
         * @param object 将要进入的view，由instantiateItem方法创建
         * @return true表示不去创建，使用缓存。false：重新创建
         */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view ==object;
        }

        /**
         *类似于BaseAdapter的getView方法
         * 用于将数据设置给view
         * 由于他最多3个界面，不需要ViewHolder
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = View.inflate(MainActivity.this,R.layout.adapter_ad,null);
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_image);

            Ad ad = list.get(position%list.size());
            imageView.setImageResource(ad.getIconResId());
            container.addView(view);//一定不能少，将view加入到ViewPager中
            return view;
        }

        /**
         * 销毁page
         * @param container
         * @param position 当前需要销毁第几个page
         * @param object 当前需要消耗的page
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
            container.removeView((View) object);
        }
    }
}
